import { _decorator, Component, Node, EventGamepad, input, Input, EventHandle, game, Vec2, v2, director, Game } from 'cc';
import CocosGlobalInput from './CocosGlobalInput';
import { IGamepadCtrl } from '../core/IGamepadCtrl';
class DigitalKey {
    id: number = 0;
    bState: number = 0;
}
class AnalogKey {
    x: number = 0;
    y: number = 0;
    eMode: number = 0;
}
let UP = "Up"
let LEFT = "Left"
let RIGHT = "Right"
let DOWN = "Down"
export class CCGamepadCtrl implements IGamepadCtrl {


    //数字按键相关
    protected digitalNames: string[]
    protected m_digitalHandles: DigitalKey[] = []
    protected m_digitalActionData: number[] = []
    protected digitalChangeData: number[] = []

    //摇杆数据
    protected analogNames: string[]
    protected m_analogActionData: number[] = []
    protected analogActionData: number[] = []
    protected m_analogHandles: AnalogKey[] = []
    protected analogChangeData: number[] = []

    protected input: CocosGlobalInput = CocosGlobalInput.getInstance();

    protected _active: boolean = false;
    isActive() {
        return this._active;
    }

    getDigitalChangeData() {
        this.digitalChangeData.length = 0;

        for (let index = 0; index < this.m_digitalHandles.length; index++) {
            const data = this.m_digitalHandles[index];
            if (data.bState != this.m_digitalActionData[index]) {
                this.digitalChangeData.push(index)
                this.digitalChangeData.push(data.bState)
                this.m_digitalActionData[index] = data.bState;
            }

        }

        return this.digitalChangeData;
    }

    getAnalogChangeData() {
        this.analogChangeData.length = 0;
        let pos = 0;
        for (let index = 0; index < this.m_analogHandles.length; index++) {
            const data = this.m_analogHandles[index];
            if (data.x != this.m_analogActionData[pos] || data.y != this.m_analogActionData[pos + 1]) {
                this.analogChangeData.push(pos >> 1);
                this.analogChangeData.push(data.x)
                this.analogChangeData.push(data.y);
                this.m_analogActionData[pos] = data.x;
                this.m_analogActionData[pos + 1] = data.y;
            }
            pos += 2;
        }
        return this.analogChangeData;
    }

    getAnalogActionData(index: number) {
        this.analogActionData.length = 0;
        let b: AnalogKey = this.m_analogHandles[index]
        if (b) {
            this.analogActionData.push(b.x)
            this.analogActionData.push(b.y)
            this.analogActionData.push(index)
        }
        return this.analogActionData;
    }

    init(digitalNames: string[], analogNames: string[]) {
        // console.log(' CCGamepadMgr digitalNames ', digitalNames)
        this.digitalNames = digitalNames;
        this.analogNames = analogNames;
        for (let index = 0; index < this.digitalNames.length; index++) {
            this.m_digitalHandles[index] = new DigitalKey()
            this.m_digitalActionData[index] = 0;
        }

        for (let index = 0; index < this.analogNames.length; index++) {
            this.m_analogHandles[index] = new AnalogKey()
        }
        this.input.on(Input.EventType.GAMEPAD_CHANGE, this.gamePadChange, this)
        this.input.on(Input.EventType.GAMEPAD_INPUT, this.gamePadInput, this)


        this.input.on(Input.EventType.HANDLE_INPUT, this.handleInput, this)
        this.input.on(Input.EventType.HANDLE_POSE_INPUT, this.handlePoseInput, this)
    }

    clear() {
        this.m_digitalHandles.length = 0;
        this.m_digitalActionData.length = 0;
        this.m_analogHandles.length = 0;
        this.input.off(Input.EventType.GAMEPAD_CHANGE, this.gamePadChange, this)
        this.input.off(Input.EventType.GAMEPAD_INPUT, this.gamePadInput, this)

        this.input.off(Input.EventType.HANDLE_INPUT, this.handleInput, this)
        this.input.off(Input.EventType.HANDLE_POSE_INPUT, this.handlePoseInput, this)
    }


    gamePadInput(event: EventGamepad) {
        // console.log("gamePadInput  ", event)
        // if (event.type == 'gamepad-change') {
        this.printEvent(event, 'gamePadInput===================')
        // }    

    }

    printEvent(event: EventGamepad, log: string) {
        this._active = true;
        // console.log(log)
        let gamepad = event.gamepad;
        // console.log('gamePadInput ', event.gamepad)
        let deviceId = gamepad.deviceId;
        // console.log(' deviceId ', deviceId)


        // this.m_digitalHandles.length = 0;
        for (let index = 0; index < this.digitalNames.length; index++) {
            const element = this.digitalNames[index];
            let data = this.m_digitalHandles[index];
            let dir: Vec2 = gamepad.dpad.getValue();
            switch (element) {
                case UP:
                    //up
                    if (dir.y == 1) {//9
                        data.bState = 1;
                    } else {
                        data.bState = 0;
                    }
                    break;
                case LEFT:
                    if (dir.x == -1) {//9
                        data.bState = 1;
                    } else {
                        data.bState = 0;
                    }
                    break;
                case RIGHT:
                    if (dir.x == 1) {//9
                        data.bState = 1;
                    } else {
                        data.bState = 0;
                    }
                    break;
                case DOWN:
                    if (dir.y == -1) {//9
                        data.bState = 1;
                    } else {
                        data.bState = 0;
                    }
                    break;
                default:
                    let s = gamepad[element].getValue()
                    if (s == 1) {
                        data.bState = 1
                    } else {
                        data.bState = 0
                    }
                    break;

            }
            // console.log('keyName ', element, 'state ', data.bState, ' keyCode ', index)

        }

        for (let index = 0; index < this.analogNames.length; index++) {
            const element = this.analogNames[index];
            let data: AnalogKey = this.m_analogHandles[index]
            let pos = gamepad[element].getValue();
            data.x = pos.x;
            data.y = pos.y;
            // console.log('keyName ', element, 'pos ', pos, ' keyCode ', index)
        }
    }

    gamePadChange(event: EventGamepad) {
        // console.log("gamePadChange  ", event)
        if (event.gamepad.connected) {
            if (game.isPaused()) {
                game.emit(Game.EVENT_SHOW)
                // GEvent.getInstance().emit(GEvent.GAME_RESUME)
            }
        } else {
            if (!game.isPaused()) {
                game.emit(Game.EVENT_HIDE)
                // GEvent.getInstance().emit(GEvent.GAME_PAUSE)
            }
        }

        if (!game.isPaused()) {
            this.printEvent(event, 'gamePadChange&&&&&&&&&&&&&&&&&&&&&&&&')
        }
    }

    handlePoseInput(event: EventHandle) {
        console.log("handlePoseInput  ", event)
    }

    handleInput(event: EventHandle) {
        console.log("handleInput  ", event)
    }

    update() {

    }
}


